﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using dhDataAcess.Model;

namespace dhDataAcess.sysDAL
{
  public  class accessGroupDAL
    {
      string tableName = "accessGroup";
          private SqlTransaction tran = null;
        private SqlConnection conn = null;
        public accessGroupDAL(SqlTransaction _transaction, SqlConnection _conn)
        {
            tran = _transaction;
            conn=_conn;
        }
      
        /// <summary>
        /// 插入一行权限组表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>

        public int accessGroupInsert(accessGroupModel  deptRow)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string insertStr = "exec accessGroupInsert @groupNum,@groupName";
            //string insertStr = baseBLL.dTableColumnRead.insertSqlStr(tableName).ToString();
           // string insertStr = "insert into accessGroup (groupNum,groupName,createDate,createUser,updateDate,updateUser,mark) values(@groupNum,@groupName,@createDate,@createUser,@updateDate,@updateUser,@mark)";
            SqlParameter[] para = {
                                   new SqlParameter ("@groupNum",deptRow .groupNum ),
                                   new SqlParameter ("@groupName",deptRow .groupName ),
                                   new SqlParameter ("@createDate",deptRow .createDate ),
                                   new SqlParameter ("@createUser",deptRow .updateUser ),
                                   new SqlParameter ("@updateDate",deptRow .updateDate ),
                                   new SqlParameter ("@updateUser",deptRow .updateUser ),
                                   new SqlParameter ("@mark",deptRow .mark )
                                 };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = insertStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }

        /// <summary>
        /// 更新一行权限表数据 
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>

        public int accessGroupUpdate(accessGroupModel deptRow)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string updateStr = baseBLL.dTableColumnRead.updateSqlStr(tableName).ToString();
            //string updateStr = "update accessGroup set mark=@mark,updateUser=@updateUser,updateDate=@updateDate,createUser=@createUser,createDate=@createDate,groupName=@groupName where groupNum=@groupNum";
            SqlParameter[] para = {
                                   new SqlParameter ("@groupNum",deptRow .groupNum  ),
                                   new SqlParameter ("@groupName",deptRow .groupName ),
                                   new SqlParameter ("@createDate",deptRow .createDate ),
                                   new SqlParameter ("@createUser",deptRow .updateUser ),
                                   new SqlParameter ("@updateDate",deptRow .updateDate ),
                                   new SqlParameter ("@updateUser",deptRow .updateUser ),
                                   new SqlParameter ("@mark",deptRow .mark )};
           
            cmd.Parameters.AddRange(para);
            cmd.CommandText = updateStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }


        /// <summary>
        /// 删除一行权限表数据
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>

        public int accessGroupDel(string groupNum)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string deleteStr = baseBLL.dTableColumnRead.delSqlStr(tableName).ToString();
           // string deleteStr = "delete accessGroup where groupNum=@groupNum";
            SqlParameter[] para = {
                                    new SqlParameter ("@groupNum",groupNum)
                                  };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = deleteStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 根据权限组编号返回某一权限组名数据
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>

        public string accessGroupretname(string groupNum)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string selectStr = "selete groupName from accessGroup where groupNum=@groupNum";
            SqlParameter[] para = {
                                new SqlParameter ("@deptNum",groupNum),
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = selectStr;
            cmd.Transaction = tran;
            SqlDataReader rdr = cmd.ExecuteReader();
            string groupName = null;
            while (rdr.Read())
            {
                groupName = rdr[0].ToString();
            }

            return groupName;
        }
    }
}
